package com.example.util.login;
 
import com.example.pojo.redis.Student;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
 
/**
 * Redis实现排行榜
 * 实现博客积分排名
 * @author pan_junbiao
 **/
public class RankingList
{
    static int TOTAL_SIZE = 10; //用户总人数
 
    public static void main(String[] args)
    {
        //创建Jedis实例
        Jedis jedis = new Jedis("47.104.155.187",6388);
        jedis.auth("test123");
        System.out.println("Redis连接成功");
 
        try
        {
            //Redis键
            String key = "BLOG_KEY";
 
            //清理可能已有的键
            jedis.del(key);

            //模拟生成多个博主
            List<String> bloggers = new ArrayList<>();
            for(int i=1; i<=TOTAL_SIZE; i++)
            {
                bloggers.add("pan_junbiao的博客_" + i);
            }
            //模拟生成多个博主
            List<Student> bloggerss = new ArrayList<>();
            //创建学生对象
            Student s1 = new Student("Tom",4);
            Student s2 = new Student("Bob",6);
            Student s3 = new Student("Mia",5);

            //把学生添加到集合
            bloggerss.add(s1);
            bloggerss.add(s2);
            bloggerss.add(s3);

            //遍历集合
            Iterator<Student> it = bloggerss.iterator();
            while(it.hasNext()){
                Student s = it.next();
                System.out.println(s.getName()+','+s.getNum());
            }
            //记录每个博主的积分
            for(int j=0; j<bloggers.size(); j++) {
                //模拟生成博主的博客积分
                int score = (int) (Math.random() * 100);
                String blogger = bloggers.get(j);
 
                //打印博主的博客积分信息
                System.out.println("博客ID：" + blogger + "，博客积分：" + score);
 
//                //将博主信息与博客积分保存到Redis中
                jedis.zadd("key", score, blogger);
            }
 
            //打印全部博客的积分排行榜
            System.out.println();
            System.out.println("-----------全部博客排行榜-----------");
            Set<Tuple> scoreList = jedis.zrevrangeWithScores(key,0,-1);
            for(Tuple item : scoreList)
            {
                System.out.println("博客ID：" + item.getElement() + "，博客积分：" + item.getScore());
            }
 
            //打印排名前五名的博客
            System.out.println();
            System.out.println("-----------排名前五名的博客排行榜-----------");
            scoreList = jedis.zrevrangeWithScores(key,0,4);
            for(Tuple item : scoreList)
            {
                System.out.println("博客ID：" + item.getElement() + "，博客积分：" + item.getScore());
            }
 
            //打印积分在5000至8000分之间的博客
            System.out.println();
            System.out.println("-----------积分在5000至8000分之间的博客-----------");
            scoreList = jedis.zrangeByScoreWithScores(key,5000,8000);
            for(Tuple item : scoreList)
            {
                System.out.println("博客ID：" + item.getElement() + "，博客积分：" + item.getScore());
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            jedis.quit();
            jedis.close();
        }
    }
}